import { InfoCircleOutlined } from '@ant-design/icons';
import { Col, Row, Tooltip } from 'antd';
import { useEffect, useState } from 'react';
import TradeCard from '../components/TradeCard';

const topColResponsiveProps = {
  xs: 24,
  sm: 24,
  md: 24,
  lg: 24,
  xl: 8,
  style: { marginBottom: 12 },
};
type PirceContrast = {
  weight: number;
  cityCode: string[];
  onchange: (lowValue: number, lowName: string) => void;
};
export default (porps: PirceContrast) => {
  const { weight, cityCode, onchange } = porps;
  const [lastPriceValue, setLastPriceValue] = useState<number[]>([]);
  const [lowPrice, setLowPrice] = useState<[boolean, boolean, boolean]>([false, false, false]);

  function specialOffer(city: number) {
    const ReviseWeight = () => {
      if (weight <= 100) {
        if (Number((weight % 1).toFixed(2)) < 0.5 && Number((weight % 1).toFixed(2)) > 0) {
          return Math.round(weight) + 0.5;
        } else {
          return weight;
        }
      } else {
        return weight;
      }
    };
    const price = (FW: number, SW: number, TW: number) => {
      const reviseWeight = ReviseWeight();
      if (reviseWeight <= 1 && reviseWeight > 0) {
        return FW;
      } else if (reviseWeight <= 30) {
        return FW + (reviseWeight - 1) * SW;
      } else if (30 < reviseWeight) {
        return FW + 29 * SW + (reviseWeight - 30) * TW;
      }
      return 0;
    };
    switch (city) {
      case 330100:
        return price(10, 2, 3);
      case 330200:
      case 330300:
      case 330400:
      case 330500:
      case 330600:
      case 330700:
      case 330800:
      case 330900:
      case 331000:
      case 331100:
      case 310000:
      case 320100:
      case 320200:
      case 320400:
      case 320500:
      case 320600:
      case 320800:
      case 320900:
      case 321000:
      case 321100:
      case 321200:
        return price(12, 2, 3);
      case 321300:
      case 320300:
      case 320700:
      case 360100:
      case 360200:
      case 360300:
      case 360400:
      case 360500:
      case 360600:
      case 360700:
      case 360800:
      case 360900:
      case 361000:
      case 361100:
      case 350100:
      case 350200:
      case 350300:
      case 350400:
      case 350500:
      case 350600:
      case 350700:
      case 350800:
      case 350900:
      case 340100:
      case 340200:
      case 340300:
      case 340400:
      case 340500:
      case 340600:
      case 340700:
      case 340800:
      case 341000:
      case 341100:
      case 341200:
      case 341300:
      case 341500:
      case 341600:
      case 341700:
      case 341800:
      case 410100:
      case 410400:
      case 410700:
      case 410800:
      case 410900:
      case 411000:
      case 411100:
      case 411200:
      case 411300:
      case 411400:
      case 411500:
      case 411600:
      case 411700:
      case 430200:
      case 420100:
      case 420200:
      case 420600:
      case 420700:
      case 420800:
      case 420900:
      case 421000:
      case 421100:
      case 421200:
      case 421300:
      case 422800:
      case 370100:
      case 370200:
      case 370300:
      case 370400:
      case 370500:
      case 370600:
      case 370700:
      case 370800:
      case 370900:
      case 371100:
      case 371300:
      case 371500:
      case 371600:
      case 371700:
        return price(15, 5, 6);
      case 411200:
      case 410300:
      case 419001:
      case 410600:
      case 410500:
      case 430100:
      case 430300:
      case 430400:
      case 430500:
      case 430600:
      case 430700:
      case 430800:
      case 430900:
      case 431000:
      case 431100:
      case 431200:
      case 431300:
      case 433100:
      case 422800:
      case 420500:
      case 420300:
      case 429021:
      case 371000:
      case 371400:
      case 110000:
      case 440100:
      case 440200:
      case 440300:
      case 440400:
      case 440500:
      case 440600:
      case 440700:
      case 440800:
      case 440900:
      case 441200:
      case 441300:
      case 441400:
      case 441500:
      case 441600:
      case 441700:
      case 441800:
      case 441900:
      case 442000:
      case 445100:
      case 445200:
      case 445300:
      case 450100:
      case 450200:
      case 450300:
      case 450400:
      case 450500:
      case 450600:
      case 450700:
      case 450800:
      case 450900:
      case 451000:
      case 451100:
      case 451200:
      case 451300:
      case 451400:
      case 520100:
      case 520200:
      case 520300:
      case 520400:
      case 520500:
      case 520600:
      case 522300:
      case 522600:
      case 522700:
      case 210100:
      case 210200:
      case 210300:
      case 210400:
      case 210500:
      case 210600:
      case 210700:
      case 210800:
      case 210900:
      case 211000:
      case 211100:
      case 211200:
      case 211300:
      case 211400:
      case 610100:
      case 610200:
      case 610300:
      case 610400:
      case 610500:
      case 610600:
      case 610700:
      case 610800:
      case 610900:
      case 611000:
      case 640100:
      case 640200:
      case 640300:
      case 640400:
      case 640500:
      case 140100:
      case 140200:
      case 140300:
      case 140400:
      case 140500:
      case 140600:
      case 140700:
      case 140800:
      case 140900:
      case 141000:
      case 141100:
      case 120000:
      case 500000:
      case 620500:
      case 621000:
      case 620800:
      case 150100:
      case 150200:
      case 150300:
      case 150400:
      case 150500:
      case 150600:
      case 150800:
      case 150900:
      case 152200:
      case 152500:
      case 152900:
      case 510100:
      case 510300:
      case 510500:
      case 510600:
      case 510700:
      case 510800:
      case 510900:
      case 511000:
      case 511100:
      case 511300:
      case 511400:
      case 511500:
      case 511600:
      case 511700:
      case 511900:
      case 512000:
      case 513200:
      case 513300:
      case 130100:
      case 130200:
      case 130300:
      case 130400:
      case 130500:
      case 130600:
      case 130700:
      case 130800:
      case 130900:
      case 131000:
      case 131100:
        return price(16, 6, 7);
      case 620100:
      case 620200:
      case 620300:
      case 620400:
      case 620600:
      case 620700:
      case 620900:
      case 621100:
      case 621200:
      case 622900:
      case 623000:
      case 150700:
      case 511800:
      case 510400:
      case 513400:
      case 460100:
      case 460200:
      case 460300:
      case 460400:
      case 530100:
      case 530300:
      case 530400:
      case 530500:
      case 530600:
      case 530700:
      case 530800:
      case 530900:
      case 532300:
      case 532500:
      case 532600:
      case 532800:
      case 532900:
      case 533100:
      case 533300:
      case 533400:
      case 630100:
      case 630200:
      case 632200:
      case 632300:
      case 632500:
      case 632600:
      case 632700:
      case 632800:
      case 220100:
      case 220200:
      case 220300:
      case 220400:
      case 220500:
      case 220600:
      case 220700:
      case 220800:
      case 222400:
      case 230100:
      case 230200:
      case 230300:
      case 230400:
      case 230500:
      case 230600:
      case 230700:
      case 230800:
      case 230900:
      case 231000:
      case 231100:
      case 231200:
      case 232700:
        return price(17, 6, 7);
      case 650100:
      case 650200:
      case 650400:
      case 650500:
      case 652300:
      case 652700:
      case 652800:
      case 652900:
      case 653000:
      case 653100:
      case 653200:
      case 654000:
      case 654200:
      case 654300:
        return price(20, 10, 11);
      case 540100:
      case 540200:
      case 540300:
      case 540400:
      case 540500:
      case 540600:
      case 542500:
        return price(24, 12, 13);
      default:
        return 0;
    }
  }

  function moreBroad(city: number) {
    const price = (FW: number, SW: number, TW: number, low: number) => {
      if (weight <= 30) {
        return low;
      } else if (weight <= 50) {
        if (10 + (weight - 1) * FW < low) {
          return low;
        } else {
          return 10 + (weight - 1) * FW;
        }
      } else if (weight <= 100) {
        if (10 + (weight - 1) * SW < low) {
          return low;
        } else {
          return 10 + (weight - 1) * SW;
        }
      } else if (weight > 100) {
        return 10 + (weight - 1) * TW;
      } else {
        return 0;
      }
    };
    switch (city) {
      case 310000:
        return price(1.2, 1.05, 0.95, 50);
      case 320100:
      case 320200:
      case 320300:
      case 320400:
      case 320500:
      case 320600:
      case 320700:
      case 320800:
      case 320900:
      case 321000:
      case 321100:
      case 321200:
      case 321300:
        return price(1.5, 1.2, 1.15, 50);
      case 330100:
      case 330200:
      case 330300:
      case 330400:
      case 330500:
      case 330600:
      case 330700:
      case 330800:
      case 331000:
      case 331100:
        return price(1.5, 1.2, 1.15, 50) * 1.059;
      case 340100:
      case 340200:
      case 340300:
      case 340400:
      case 340500:
      case 340600:
      case 340700:
      case 340800:
      case 341000:
      case 341100:
      case 341200:
      case 341300:
      case 341500:
      case 341600:
      case 341700:
      case 341800:
        return price(1.5, 1.3, 1.25, 60);
      case 110000:
      case 120000:
      case 441900:
      case 440100:
      case 440300:
      case 440400:
      case 442000:
      case 441300:
      case 440600:
      case 441200:
      case 440700:
      case 350200:
      case 350100:
      case 350500:
        return price(2.05, 1.8, 1.65, 60);
      case 350700:
      case 350400:
      case 350800:
      case 350600:
      case 350900:
      case 350300:
        return price(2.2, 2.15, 1.95, 80);
      case 610100:
      case 610400:
        return price(1.9, 1.9, 1.85, 80);
      case 445200:
      case 440900:
      case 441400:
      case 441800:
      case 441500:
      case 440500:
      case 440200:
      case 441700:
      case 445300:
      case 440800:
      case 441600:
      case 445100:
        return price(2.25, 2.15, 1.95, 80);
      case 451400:
      case 450100:
        return price(2.25, 2.05, 1.95, 80);
      case 511181:
      case 511400:
      case 510100:
      case 512000:
      case 500000:
      case 510700:
        return price(2.35, 2.25, 2.25, 80);
      case 450200:
      case 451300:
      case 450300:
      case 520100:
      case 520300:
      case 520400:
        return price(2.76, 2.65, 2.65, 80);
      case 130400:
      case 130600:
      case 130900:
      case 130300:
      case 130200:
      case 130800:
      case 131100:
      case 130500:
      case 130700:
      case 130100:
      case 131000:
        return price(2.35, 2.35, 2.25, 80);
      case 410200:
      case 410100:
      case 411000:
      case 410700:
      case 411500:
      case 410600:
      case 410300:
        return price(2.45, 2.35, 2.25, 80);
      case 230100:
      case 220100:
      case 140100:
      case 140700:
      case 530100:
        return price(3, 2.85, 2.75, 80);
      case 420100:
      case 420700:
      case 360100:
        return price(2, 1.8, 1.65, 80);
      case 420800:
      case 421000:
      case 420600:
      case 420500:
      case 421300:
      case 420900:
      case 420300:
      case 429021:
      case 420200:
      case 360400:
      case 360700:
      case 360800:
      case 361000:
      case 360200:
      case 360300:
      case 361100:
      case 360500:
      case 360900:
      case 360600:
        return price(2.35, 2.15, 2.05, 80);
      case 430300:
      case 430100:
      case 430200:
        return price(2.2, 2.2, 2, 80);
      case 430900:
      case 431200:
        return price(2.35, 2.25, 2.05, 80);
      case 220200:
        return price(3.55, 3.25, 3.1, 80);
      case 210200:
      case 210500:
      case 210400:
      case 210100:
      case 211200:
      case 210300:
      case 340500:
        return price(4.2, 3.5, 3.2, 80);
      case 150100:
      case 650100:
      case 150200:
      case 630100:
        return price(10, 6, 5.5, 80);
      case 370200:
      case 370100:
      case 370600:
      case 371000:
      case 370700:
      case 371400:
      case 371600:
      case 370500:
      case 371700:
      case 370800:
      case 371500:
      case 371300:
      case 371100:
      case 370900:
      case 370400:
      case 370300:
        return price(2.2, 2.05, 1.85, 80);
      case 610300:
      case 610200:
        return price(3.8, 3.5, 3.3, 80);
      default:
        return 0;
    }
  }

  function JingDong(city: number) {
    const price = (FW: number, SW: number, TW: number) => {
      const lastWeight = Math.round(weight);
      if (lastWeight <= 20) {
        return FW;
      } else if (lastWeight <= 100) {
        return FW + (lastWeight - 20) * SW;
      } else if (lastWeight > 100) {
        return FW + 80 * SW + (lastWeight - 100) * TW;
      } else {
        return 0;
      }
    };
    switch (city) {
      case 330100:
        return price(30, 1.7, 2.7);
      case 321100:
      case 320800:
      case 320803:
      case 320400:
      case 321000:
      case 321200:
      case 320300:
        return price(32, 1.9, 3);
      case 331000:
        return price(33, 1.8, 2.8);
      case 320500:
      case 321300:
      case 320200:
      case 320600:
      case 320900:
      case 320100:
      case 320700:
        return price(33, 1.9, 3);
      case 310000:
        return price(33, 2.2, 3.1);
      case 331100:
      case 330500:
      case 330700:
      case 330200:
      case 330600:
      case 330400:
      case 330300:
        return price(34, 1.8, 2.8);
      case 341100:
      case 340200:
      case 340221:
      case 341700:
      case 340400:
      case 340700:
      case 340500:
        return price(34, 2.2, 3.2);
      case 340800:
      case 340300:
      case 341800:
      case 341500:
      case 341600:
      case 341000:
      case 341300:
      case 340100:
      case 341200:
      case 340600:
        return price(35, 2.2, 3.2);
      case 330800:
        return price(36, 1.8, 2.8);
      case 330900:
        return price(37, 1.8, 2.8);
      case 360100:
      case 360200:
      case 360300:
      case 360400:
      case 360500:
      case 360600:
      case 360700:
      case 360800:
      case 360900:
      case 361000:
      case 361100:
        return price(85, 2.9, 3.9);
      case 350100:
      case 350200:
      case 350300:
      case 350400:
      case 350500:
      case 350600:
      case 350700:
      case 350800:
      case 350900:
      case 370700:
      case 370100:
      case 370400:
      case 371600:
      case 371700:
      case 370200:
      case 371300:
      case 371100:
      case 371500:
      case 370900:
      case 371400:
      case 370300:
      case 370500:
      case 370502:
      case 411500:
      case 410400:
      case 411400:
      case 411700:
      case 411300:
      case 411600:
      case 411000:
      case 411100:
      case 410900:
      case 410928:
      case 410100:
      case 410700:
      case 410721:
      case 420900:
      case 421300:
      case 421200:
      case 429006:
      case 429004:
      case 420100:
      case 429005:
      case 421000:
      case 421003:
      case 420700:
      case 421100:
      case 420800:
      case 420200:
      case 420202:
        return price(88, 2.9, 3.9);
      case 430300:
      case 430600:
      case 430100:
      case 430900:
      case 431300:
      case 430200:
      case 441400:
        return price(88, 3.7, 4.7);
      case 371000:
      case 370600:
      case 429021:
      case 422800:
      case 420500:
      case 420300:
      case 420600:
      case 410500:
      case 410522:
      case 410800:
      case 419001:
      case 410300:
      case 410600:
      case 411200:
        return price(89, 2.9, 3.9);
      case 140100:
      case 140200:
      case 140300:
      case 140400:
      case 140500:
      case 140600:
      case 140700:
      case 140800:
      case 140900:
      case 141000:
      case 141100:
      case 110000:
      case 130100:
      case 130200:
      case 130300:
      case 130400:
      case 130500:
      case 130600:
      case 130700:
      case 130800:
      case 130900:
      case 131000:
      case 131100:
      case 120000:
      case 610100:
      case 610200:
      case 610300:
      case 610400:
      case 610500:
      case 610600:
      case 610700:
      case 610800:
      case 610900:
      case 611000:
      case 441700:
      case 445200:
      case 440700:
      case 441300:
      case 441900:
      case 440200:
      case 445300:
      case 440500:
      case 440900:
      case 440100:
      case 210202:
      case 442000:
      case 441500:
      case 440800:
      case 445100:
      case 441200:
      case 441600:
      case 441800:
      case 440300:
      case 440400:
      case 430400:
      case 430421:
      case 430500:
      case 430523:
      case 430800:
      case 431200:
      case 430700:
      case 431100:
      case 431000:
        return price(89, 3.7, 4.7);
      case 210100:
      case 210200:
      case 210300:
      case 210400:
      case 210500:
      case 210600:
      case 210700:
      case 210800:
      case 210900:
      case 211000:
      case 211100:
      case 211200:
      case 211300:
      case 211400:
      case 510100:
      case 510300:
      case 510400:
      case 510500:
      case 510600:
      case 510700:
      case 510800:
      case 510900:
      case 511000:
      case 511100:
      case 511300:
      case 511400:
      case 511500:
      case 511600:
      case 511700:
      case 511800:
      case 511900:
      case 512000:
      case 513200:
      case 513300:
      case 513400:
      case 469027:
      case 469006:
      case 460100:
      case 469023:
      case 469005:
      case 469007:
      case 469026:
      case 469022:
      case 469001:
      case 469029:
      case 469021:
      case 469002:
      case 469028:
      case 469025:
      case 469024:
      case 460200:
      case 460400:
      case 469030:
      case 532500:
      case 532529:
      case 532300:
      case 532301:
      case 532900:
      case 532901:
      case 533400:
      case 532600:
      case 532601:
      case 533300:
      case 533100:
      case 532800:
      case 450400:
      case 450421:
      case 451100:
      case 640122:
      case 620200:
      case 621100:
      case 230225:
      case 623000:
      case 620900:
      case 622900:
      case 622901:
      case 622921:
      case 522300:
      case 522600:
      case 522700:
        return price(90, 3.7, 4.7);
      case 222400:
      case 632200:
      case 632800:
      case 632300:
      case 630200:
        return price(90, 3.9, 4.9);
      case 500000:
        return price(91, 3.9, 4.9);
      case 451300:
      case 450700:
      case 450702:
      case 450703:
      case 533422:
      case 450500:
      case 450100:
      case 450600:
      case 450300:
      case 450900:
      case 450800:
      case 141125:
      case 220524:
      case 450200:
      case 450204:
      case 450205:
      case 450206:
      case 450222:
      case 451400:
      case 451200:
      case 520100:
      case 520500:
      case 520200:
      case 520400:
      case 530900:
      case 530100:
      case 530400:
      case 530300:
        return price(92, 3.7, 4.7);
      case 620100:
      case 620500:
      case 620400:
      case 620402:
      case 621000:
      case 620600:
      case 621200:
      case 620300:
      case 620700:
        return price(93, 3.7, 4.7);
      case 630100:
        return price(93, 3.9, 4.9);
      case 530500:
      case 530700:
      case 530600:
      case 530800:
      case 451000:
      case 520300:
      case 520600:
      case 620800:
        return price(95, 3.7, 4.7);
      case 220500:
      case 220521:
      case 220300:
      case 220200:
      case 220600:
      case 220800:
      case 220100:
      case 220400:
      case 220700:
        return price(95, 3.9, 4.9);
      case 460300:
      case 632700:
      case 632500:
      case 632600:
        return price(125, 3.9, 4.9);
      case 230100:
      case 230200:
      case 230300:
      case 230400:
      case 230500:
      case 230600:
      case 230700:
      case 230800:
      case 230900:
      case 231000:
      case 231100:
      case 231200:
      case 232700:
      case 640100:
      case 640200:
      case 640300:
      case 640400:
      case 640500:
      case 150100:
      case 150800:
      case 150200:
      case 150900:
      case 150600:
      case 152900:
      case 150300:
        return price(127, 3.7, 4.7);
      case 150500:
      case 150700:
      case 152500:
      case 152200:
      case 150400:
        return price(128, 3.7, 4.7);
      case 540000:
      case 652300:
      case 652301:
      case 650500:
      case 654000:
      case 652800:
      case 652700:
        return price(230, 6.2, 8);
      case 654200:
      case 659006:
      case 653100:
      case 654300:
      case 652900:
      case 659004:
      case 653200:
      case 659003:
      case 653000:
      case 659002:
      case 659009:
      case 650200:
      case 650203:
      case 659001:
      case 650100:
      case 650121:
        return price(235, 6.2, 8);
      default:
        return 0;
    }
  }

  const PriceComparison = () => {
    const lastPrice: number[] = [];
    const numberCity = Number(cityCode[0]);
    if (!!cityCode[0] && weight) {
      lastPrice.push(specialOffer(numberCity));
      lastPrice.push(moreBroad(numberCity));
      lastPrice.push(JingDong(numberCity));
      return [lastPrice[0] * 0.8, lastPrice[1] * 0.85, Math.round(lastPrice[2]) * 0.8];
    }
    return [0, 0, 0];
  };
  useEffect(() => {
    setLastPriceValue(PriceComparison());
  }, [weight, cityCode]);

  //比价函数
  const colorComparision = () => {
    // const Contrast = _.cloneDeep(lastPriceValue)    //这里一定要深拷贝
    const Contrast = [lastPriceValue[0], lastPriceValue[1], lastPriceValue[2]];

    Contrast.sort(function (a, b) {
      return a - b;
    }).join(',');
    for (let i = 0; i < Contrast.length; i++) {
      if (Contrast[0] == 0) {
        Contrast[0] = Contrast[i];
      }
    }
    if (Number(String(Contrast[0]).split(',')[0]) == lastPriceValue[0] && !!lastPriceValue[0]) {
      onchange(Contrast[0], '特惠送京东');
      setLowPrice([true, false, false]);
    } else if (
      Number(String(Contrast[0]).split(',')[0]) == lastPriceValue[1] &&
      !!lastPriceValue[1]
    ) {
      onchange(Contrast[0], '跨越速运');
      setLowPrice([false, true, false]);
    } else if (
      Number(String(Contrast[0]).split(',')[0]) == lastPriceValue[2] &&
      !!lastPriceValue[2]
    ) {
      onchange(Contrast[0], '特快重货京东');
      setLowPrice([false, false, true]);
    } else {
      onchange(0, '');
      setLowPrice([false, false, false]);
    }
  };
  useEffect(() => {
    colorComparision();
  }, [lastPriceValue]);
  return (
    <div>
      <div style={{ padding: 10 }}>
        <Row gutter={22}>
          <Col {...topColResponsiveProps}>
            <TradeCard
              titleName={'特惠送京东'}
              action={
                <Tooltip title="计费重量进位规则: 小于100kg且小数位不足0.5kg的,进位至下一个0.5kg，以上运费不包括超长超重附加费、偏远地区附加费,以及其他特殊处理费用,京东物流保留收取附加费之权利">
                  <InfoCircleOutlined style={{ fontSize: 16 }} />
                </Tooltip>
              }
              titleBar={' '}
              contentLeft={weight}
              unit={'kg'}
              contentRight={Number(lastPriceValue[0]?.toFixed(2))}
              magnify={lowPrice[0]}
            />
          </Col>
          <Col {...topColResponsiveProps}>
            <TradeCard
              titleName={'跨越速运'}
              action={
                <Tooltip title="注：省内报价9折，省外报价85折。（在报价表基础上）">
                  <InfoCircleOutlined style={{ fontSize: 16 }} />
                </Tooltip>
              }
              titleBar={' '}
              contentLeft={weight}
              unit={'kg'}
              contentRight={Number(lastPriceValue[1]?.toFixed(2))}
              magnify={lowPrice[1]}
            />
          </Col>
          <Col {...topColResponsiveProps}>
            <TradeCard
              titleName={'特快重货京东'}
              action={
                <Tooltip title="该价格是门到门一体价格，含揽收、运输、派送；赠送声明价值1000元以内保费，1000元以上声明价值按5‰收取；以上运费不包含超长超重附加费、偏远地区附加费、及其他特殊处理费用。如需提供前述服务，京东物流可能会收取附加服务费；">
                  <InfoCircleOutlined style={{ fontSize: 16 }} />
                </Tooltip>
              }
              titleBar={' '}
              contentLeft={weight}
              unit={'kg'}
              contentRight={Number(lastPriceValue[2]?.toFixed(2))}
              magnify={lowPrice[2]}
            />
          </Col>
        </Row>
      </div>
    </div>
  );
};
